<?php

if (!$l_loged) {
    include "login_error.php";
} else { if ($l_role < 10) {
        include "role_error.php";
    } else {
        ?>
<h2 class="cont_page">Změna údajů uživatele</h2>

<?PHP
if (isset($_POST['upd_btn']) && ($_POST['upd_mail'] != '') && ($_POST['upd_tel'] != '') && ($_POST['upd_name'] != '') && ($_POST['upd_role_name'] != '')) {
    if (user_role_exits($_POST['upd_role_name'])) {
        if (user_upd_admin($_GET['q'], $_POST['upd_name'], $_POST['upd_mail'], $_POST['upd_tel'], $_POST['upd_icq'], $_POST['upd_adresa'], $_POST['upd_role_name'])) {
            echo('<p class="ok_desc">Update uživatele proběhl úspěšně..</p>');
        }
    } else {
        echo('<p class="err_desc">Snažíš se přiřadit neexistující roli!!</p>');
    }

}

function fill_upd_admin($col){
    if (isset($_POST['upd_btn'])){
        return (htmlspecialchars($_POST['upd_'.$col]));
    } else {
       return user_load_value_admin($col,$_GET['q']);
    }
}

?>

<form action=<?PHP echo('"user_edit_admin.php?q='.$_GET['q'].'"'); ?> method="post" id="form_usr" name="form_usr">
    <fieldset class="user">
        <legend>Data uživatele - <?PHP echo($_GET['q']); ?></legend>
        <p <?PHP if (isset($_POST['upd_btn']) && ($_POST['upd_name'] == '')) {
            echo ('class="err_form"');
        } else {
            echo ('class="ok_form"');
        } ?>>
            <label for="upd_name" class="nes">*Jmeno: </label>
            <input maxlength="50" type="text" value="<?PHP echo(fill_upd_admin('name')); ?>" id="upd_name" name="upd_name">
        </p>
        <p <?PHP if (isset($_POST['upd_btn']) && ($_POST['upd_mail'] == '')) {echo ('class="err_form"');} else {echo ('class="ok_form"');} ?>>
            <label for="upd_mail" class="nes">*Mail: </label>
            <input type="text" maxlength="100" value="<?PHP echo(fill_upd_admin('mail')); ?>" id="upd_mail" name="upd_mail">
        </p>

        <p <?PHP if (isset($_POST['upd_btn']) && ($_POST['upd_tel'] == '')) {echo ('class="err_form"');} else {echo ('class="ok_form"');} ?>>
            <label for="upd_tel" class="nes">*Tel:</label>
            <input type="text" maxlength="9" value="<?PHP echo fill_upd_admin('tel'); ?>" id="upd_tel" name="upd_tel">
        </p>
        <p class="ok_form">
            <label for="upd_icq">ICQ: </label>
            <input type="text" maxlength="20" value="<?PHP echo fill_upd_admin('icq'); ?>" id="upd_icq" name="upd_icq">
        </p>
        <p class="ok_form">
            <label for="upd_adress">Adresa: </label>
            <input type="text" maxlength="100" size="65" value="<?PHP echo fill_upd_admin('adresa'); ?>" id="upd_adresa" name="upd_adresa">
        </p>
        <p <?PHP if (isset($_POST['upd_btn']) && ($_POST['upd_role_name'] == '')) {echo ('class="err_form"');} else {echo ('class="ok_form"');} ?>>
            <label for="upd_role_name" class="nes">*Role: </label>
            <input type="text" maxlength="20" value="<?PHP echo fill_upd_admin('role_name'); ?>" id="upd_role_name" name="upd_role_name">
        </p>
   <?PHP if (isset($_POST['upd_btn']) && (($_POST['upd_mail'] == '') || ($_POST['upd_tel'] == '') || ($_POST['upd_name'] == '') || ($_POST['upd_role_name'] == ''))) {echo ('<p class="err_desc">* Všechny prvky touto barvou je nutné vyplnit!</p>');} ?>
     <p>
        <input type="submit" value="Uložit" id="upd_btn" name="upd_btn">
    </p>
    </fieldset>
</form>

<script type="text/javascript">
    var form = document.getElementById('form_usr');
    form.onsubmit = ctrl_usr;

    function ctrl_usr(){

        var upd_name = document.getElementById('upd_name');
        var upd_mail = document.getElementById('upd_mail');
        var upd_tel = document.getElementById('upd_tel');
        var upd_p = document.getElementById('upd_role_name');


        if (upd_name.value == ''){
            alert('Chybí vypln\u011bné jméno!');
            return(true);
        }
        if (upd_mail.value == ''){
            alert('Chybí vypln\u011bné upd_mail!');
            return(true);
        }

        if (upd_tel.value == ''){
            alert('Chybí vypln\u011bné upd_tel!');
            return(true);
        }
        if (upd_p.value == ''){
            alert('Chybí vypln\u011bná role!');
            return(true);
        }

        return(true);
    }
</script>


    <?php
    }
}
?>
